#include<opencv2/opencv.hpp>
#include<iostream>
#include<vector>

using namespace cv;
using namespace std;

void drawLine(Mat &img,vector<Vec2f>lines,
double rows,double cols,Scalar scalar,int n)//检测直线并画出直线
{

Point pt1,pt2;
for(size_t i=0;i<lines.size();i++)
{
    float rho=lines[i][0];
    float theta=lines[i][1];

    double a=cos(theta);
    double b=sin(theta);
    double x0=a*rho,y0=b*rho;
    double length=max (rows,cols);

    pt1.x=cvRound(x0+length*(-b));
    pt1.y=cvRound(y0+length*(a));

    pt2.x=cvRound(x0-length*(-b));
    pt2.y=cvRound(y0-length*(a));

    line(img,pt1,pt2,scalar,n);

}


}
int main()
{

   Mat img=imread("/home/lwj/opencv.21/cz.png",IMREAD_GRAYSCALE);
   if(img.empty())
   {
    cout<<"请确认图像文件的正确性"<<endl;
    return -1;
   }
   Mat edge;
   Canny(img,edge,80,180,3,false);
    threshold(edge,edge,170,255,THRESH_BINARY);//二值化
    vector<Vec2f>lines1,lines2;
    HoughLines(edge,lines1,1,CV_PI/180,50,0,0);//小阀处理
    HoughLines(edge,lines2,1,CV_PI/180,150,0,0);//大阀值处理图像直线参数
    Mat img1,img2;
    img.copyTo(img1);
    img.copyTo(img2);
    drawLine(img1,lines1,edge.rows,edge.cols,Scalar(255),2);
    drawLine(img2,lines2,edge.rows,edge.cols,Scalar(255),2);
  imshow("edge",edge);
  imshow("img",img);
  imshow("img1",img1);
  imshow("img2",img2);
  waitKey(0);
    return 0;
}